#include "animationwindow.h"
#include "qpainter.h"
#include "qtimer.h"

AnimationWindow::AnimationWindow(QWidget *parent)
    : QWidget{parent}
{
    m_x=0;
}

void AnimationWindow::showBetScore(int bet)
{
    m_x=0;
    if(bet==1)
    {
        m_image.load(":/images/score1.png");
    }
    else if(bet==2)
    {
        m_image.load(":/images/score2.png");
    }
    else if(bet==3)
    {
        m_image.load(":/images/score3.png");
    }
    update();
    QTimer::singleShot(2000,this,&AnimationWindow::hide);//用于在指定的时间间隔后执行一次特定的操作。它通常用于延迟执行某个函数或代码块，且只执行一次
}

void AnimationWindow::showSequence(Type type)
{
    m_x=0;
    QString name=type==Pair?":/images/liandui.png":":/images/shunzi.png";
    m_image.load(name);
    update();
    QTimer::singleShot(2000,this,&AnimationWindow::hide);
}

void AnimationWindow::showJokerBomb()
{
    m_index=0;
    m_x=0;
    QTimer* timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=]{
        m_index++;
        if(m_index>8)
        {
            timer->stop();//终止定时器
            timer->deleteLater();//安全删除定时器
            m_index=8;
            hide();
        }
        QString name=QString(":/images/joker_bomb_%1.png").arg(m_index);
        m_image.load(name);
        update();
    });
    timer->start(60);
}

void AnimationWindow::showBomb(Type type)
{
    m_index=0;
    m_x=0;
    QTimer* timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=]{
        m_index++;
        if((type==Bomb&&m_index>12)||(type==JokerBomb&&m_index>8))
        {
            timer->stop();
            timer->deleteLater();
            m_index=type==Bomb?12:8;
            hide();
        }
        QString name=type==Bomb?QString(":/images/bomb_%1.png").arg(m_index):QString(":/images/joker_bomb_%1.png").arg(m_index);
        m_image.load(name);
        update();
    });
    timer->start(60);
}

void AnimationWindow::showPlane()
{
    m_x=width();
    m_image.load(":/images/plane_1.png");
    setFixedHeight(m_image.height());
    update();
    int step=width()/5;
    QTimer* timer=new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        static int dist=0;
        static int timers=0;
        dist+=5;
        if(dist>=step)
        {
            dist=0;
            timers++;
            QString name=QString(":/images/plane_%1.png").arg(timers%5+1);
            m_image.load(name);
        }
        if(m_x<=-110)
        {
            timer->stop();
            timer->deleteLater();
            dist=timers=0;
            hide();
        }
        m_x-=5;
        update();
    });
    timer->start(15);
}

void AnimationWindow::paintEvent(QPaintEvent *ev)
{
    Q_UNUSED(ev)
    QPainter p(this);
    p.drawPixmap(m_x,0,m_image.width(),m_image.height(),m_image);
}
